package com.github.rxyor.ddd.service.domain.model;

import com.github.rxyor.ddd.service.infrastructure.common.enums.OrderStatusEnum;
import com.github.rxyor.ddd.service.infrastructure.repository.entity.OrderAddressDO;
import com.github.rxyor.ddd.service.infrastructure.repository.entity.OrderDO;
import com.github.rxyor.ddd.service.interfaces.repository.OrderRepository;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.springframework.beans.BeanUtils;

/**
 *<p>
 *
 *</p>
 *
 * @author liuyang
 * @date 2019/10/3 周四 16:21:00
 * @since 1.0.0
 */
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = false)
public class Order extends OrderDO {

    private final OrderRepository orderRepository;

    public Order(OrderRepository orderRepository) {
        this.orderRepository = orderRepository;
    }

    private OrderAddressDO orderAddress;

    public boolean isDeleted() {
        return OrderStatusEnum.DELETED.getCode().equals(getStatus());
    }

    public Order load(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("订单ID不能为空");
        }
        OrderDO orderDO = orderRepository.selectOrderById(id);
        OrderAddressDO orderAddressDO = orderRepository.selectOrderAddressByOrderId(id);
        BeanUtils.copyProperties(orderDO, this);
        this.orderAddress = orderAddressDO;
        return this;
    }

}
